package com.tenton.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tenton.MyException.DataNotExist;
import com.tenton.mapper.ImCommodityMapper;
import com.tenton.mapper.StockMapper;
import com.tenton.pojo.ImCommodity;
import com.tenton.pojo.Stock;
import com.tenton.service.ImCommodityService;
import com.tenton.utils.DateUtils;
import com.tenton.utils.IntegerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Date: 2021/1/9
 * @Author: Tenton
 * @Description:
 */
@Service
public class ImCommodityServiceImpl implements ImCommodityService {
    @Autowired
    ImCommodityMapper imCommodityMapper;
    @Autowired
    StockMapper stockMapper;

    /**
     * 删除进货信息
     *
     * @param id
     * @param session
     * @return
     */
    @Override
    public void deleteByPrimaryKey(Integer id,HttpSession session) {
        if (!(IntegerUtils.isEmpty(id))) {
            String name = (String) session.getAttribute("name");
            ImCommodity imCommodity = imCommodityMapper.selectByPrimaryKey(id);
            Stock stock = stockMapper.StockWithCom(imCommodity.getComId());
            //仓库中此商品的库存
            int stockNum = stock.getNum();
            //此次进货数量
            int imCommodityNum = imCommodity.getNum();
            //仓库中商品剩余库存
            stockNum = stockNum - imCommodityNum;
            stock.setNum(stockNum);
            stock.setUpdateName(name);
            Date date = new Date();
            stock.setUpdateTime(date);
            stockMapper.updateByPrimaryKey(stock);
            imCommodityMapper.deleteByPrimaryKey(id);
        } else {
            throw new NullPointerException("进货Id为空");
        }
    }

    /**
     * 新增进货信息
     *
     * @param imCommodity
     * @param session
     */
    @Override
    public void insert(ImCommodity imCommodity, HttpSession session) throws DataNotExist {
        //非空判断
        if (imCommodity != null) {
            //进货
            String name = (String) session.getAttribute("name");
            imCommodity.setCreateName(name);
            Date date = new Date();
            imCommodity.setCreateTime(date);
            imCommodityMapper.insert(imCommodity);
            //根据商品Id查询仓库中是否存在对应商品信息
            Stock stock = stockMapper.StockWithCom(imCommodity.getComId());
            //是否存在
            if (stock == null) {
                //不存在，添加
                Stock newStock = new Stock(imCommodity.getComId(), imCommodity.getComName(), imCommodity.getNum(),name,date);
                stockMapper.insert(newStock);
            } else {
                int oldNum = stock.getNum();
                int newNum = imCommodity.getNum();
                int num = oldNum + newNum;
                stock.setNum(num);
                stock.setUpdateName(name);
                stock.setUpdateTime(date);
                stockMapper.updateByPrimaryKey(stock);
            }
        } else {
            throw new DataNotExist("进货信息为空");
        }
    }

    /**
     * 获取所有进货信息
     *
     * @param model
     * @param pn
     * @return
     */
    @Override
    public void getAll(ModelMap model, Integer pn) {
        PageHelper.startPage(pn, 6);
        List<ImCommodity> imCommodities = imCommodityMapper.selectAll();
        PageInfo<ImCommodity> pageInfo = new PageInfo<ImCommodity>(imCommodities);
        model.addAttribute("pageInfo", pageInfo);
    }

    /**
     * 查询进货信息
     *
     * @param id
     * @param model
     * @return
     */
    @Override
    public void selectByPrimaryKey(Integer id, Model model) {
        if (IntegerUtils.isEmpty(id)) {
            model.addAttribute("imCommodity", imCommodityMapper.selectByPrimaryKey(id));
        } else {
            throw new NullPointerException("商品Id为空");
        }
    }

    /**
     * 查询进货信息
     *
     * @param id
     * @return
     */
    @Override
    public ImCommodity selectById(Integer id) {
        if (IntegerUtils.isEmpty(id)) {
            return imCommodityMapper.selectByPrimaryKey(id);
        } else {
            throw new NullPointerException("商品Id为空");
        }
    }

    /**
     * 商品名称模糊查询
     *
     * @param comName
     * @param model
     * @param pn
     * @return
     */
    @Override
    public void getByParams(String comName, Model model, Integer pn) {
        PageHelper.startPage(pn, 6);
        List<ImCommodity> imCommodities = imCommodityMapper.getByParams(comName);
        PageInfo<ImCommodity> pageInfo = new PageInfo<ImCommodity>(imCommodities);
        model.addAttribute("pageInfo", pageInfo);
    }
}
